<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\api\service\Token;
use app\api\model\PetsPhoto as PetsPhotoModel;
use app\api\model\PetsPhotoId as PetsPhotoIdModel;
use app\api\model\PetsPhotoAlbum;
use app\api\model\PetsPhotoComments;

class PetsPhoto extends Api
{
    public function create()
    {
        $data = input('post.');
        $uid = Token::getCurrentUid();
        $insertData = [
            'uid' => $uid,
            'content' => $data['content'],
            'type' => $data['photo_type'] == 'image' ? 1 : '2',
            'img' => $data['photo_type'] == 'image' ? $data['img'][0] : $data['video']
        ];
        $re = PetsPhotoModel::create($insertData);

        if($re) {
            $photo = [];
            if($data['photo_type'] == 'image') {
                foreach ($data['img'] as $k => $v) {
                    $photo[$k]['photo_id'] = $re->id;
                    $photo[$k]['img'] = $v;
                }
            }else{
                $photo['photo_id'] = $re->id;
                $photo['img'] = $data['video'];
            }
            model('PetsPhotoAlbum')->saveAll($photo);
            foreach ($data['pets_id'] as $v) {
                PetsPhotoIdModel::create([
                    'pets_id' => $v,
                    'photo_id' => $re->id
                ]);
            }
            return [
                'code' => 1,
                'info' => '操作成功！'
            ];
        }else{
            return [
                'code' => 0,
                'info' => '操作失败！'
            ];
        }
    }

    /**
     * 某个宠物相册列表
     * @param  [type] $pets_id [description]
     * @return [type]          [description]
     */
    public function list($pets_id) {
        $petsArr = model('PetsPhotoId')->where(['pets_id' => $pets_id])->select();
        if($petsArr) {
            $photoId = [];
            foreach ($petsArr as $v) {
                $photoId[] = $v['photo_id'];
            }
            $photo = PetsPhotoModel::with(['album'])->where('id', 'in', $photoId)->order('create_time desc')->limit(10)->select();
            foreach ($photo as $k => $v) {
                $photo[$k]['publish_day'] = date('d', strtotime($v['create_time']));
                $photo[$k]['publish_month'] = date('m', strtotime($v['create_time']));
                $photo[$k]['publish_year'] = date('Y', strtotime($v['create_time']));
                $photo[$k]['comments'] = $this->photoComments($v['id'], 1, 3);
            }
            return $photo;
        }else{
            return [];
        }
    }

    public function photoComments($photo_id, $page = 1, $size = 10) {
        $pagingComments = PetsPhotoComments::with('user')->field('id, photo_id, pets_id, uid, content, create_time, status')->where(['photo_id' => $photo_id])->order('create_time desc')->paginate($size, true, ['page' => $page])->each(function($item, $key){
            $item->nickname = $item->user->nickname;
            $item->avatar = $item->user->avatar;
            $item->time = mdate(strtotime($item->create_time));
            $item->userPets = counUserPets($item->uid);
            unset($item->user);
        });
        return $pagingComments;
        // if($pagingComments->isEmpty()) {
        //     return [
        //         'current_page' => $pagingComments->currentPage(),
        //         'data' => []
        //     ];
        // }else{
        //     $data = $pagingComments->toArray();
        //     return [
        //         'current_page' => $pagingComments->currentPage(),
        //         'data' => $data
        //     ];
        // }
    }

    /**
     * 发表评论
     * @return [type] [description]
     */
    public function postComents($pets_id) {
        $data = input('post.');
        $photo = PetsPhotoModel::get($data['id']);
        if(!$photo) {
            return [
                'code' => 0,
                'error' => '相册不存在'
            ];
        }
        $uid = Token::getCurrentUid();

        $self = model('Pets')->where(['uid' => $uid])->select();
        $data = [
            'photo_id' => $photo->id,
            'pets_id' => $pets_id,
            'uid' => $uid,
            'content' => $data['content'],
        ];
        if($self) {
            $data['status'] = 1;
        }
        $re = PetsPhotoComments::create($data);
        if($re) {
            return [
                'code' => 1,
                'info' => '评论成功'
            ];
        }else{
            return [
                'code' => 0,
                'error' => '评论失败'
            ];
        }
    }

    public function newcomments() {
        $uid = Token::getCurrentUid();
        $petsIds = model('Pets')->field('id')->where(['uid' => $uid])->select();
        $petsIdArr = [];
        if($petsIds) {
            foreach ($petsIds as $v) {
                $petsIdArr[] = $v['id'];
            }
        }
        $comments['num'] = model('PetsPhotoComments')->where('pets_id', 'in', $petsIdArr)->where(['status' => 0])->count();
        $commentsArr = PetsPhotoComments::with('user')->where('pets_id', 'in', $petsIdArr)->where(['status' => 0])->select();
        if($commentsArr) {
            foreach ($commentsArr as $k => $v) {
                $comments['data'][$k]['pets_id'] = $v['pets_id'];
                $comments['data'][$k]['nickname'] = $v['user']['nickname'];
                $comments['data'][$k]['date'] = date('m-d', strtotime($v['create_time']));
            }
        }else{
            $comments['data'] = [];
        }
        return $comments;
    }

    public function info($id) {
        $info = PetsPhotoModel::with('album')->where(['id' => $id])->find();
        if($info) {
            $info['time'] = date('Y-m-d', strtotime($info['create_time']));
            unset($info['create_time']);
            unset($info['update_time']);
            $info['comments'] = $this->photoComments($info['id'], 1, 5);
            $info['comments_num'] = db('PetsPhotoComments')->where(['photo_id' => $id])->count();
            return ['code' => 1, 'info' => 'ok', 'data' => $info];
        }else{
            return ['code' => 0, 'error' => 'error', 'data' => []];
        }
    }

    /**
     * 设置评论状态
     * @param  [type] $pets_id [description]
     * @return [type]          [description]
     */
    public function update_comments_status($pets_id) {
        $res = model('PetsPhotoComments')->save(['status' => 1], ['pets_id' => $pets_id]);
        if($res) {
            return ['code' => 1, 'info' => '修改成功'];
        }else{
            return ['code' => 0, 'error' => '修改失败'];
        }
    }
}
